package com.wframe.project.former.dataservices.floor;

import com.frame.database.JBaseDao;
import com.frame.database.JdataBean;
import com.frame.database.PageResult;
import com.wframe.project.jnd.entity.UiFloor;
import org.apache.commons.lang.StringUtils;
import org.hibernate.HibernateException;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/5/25.
 */

public class UiFloorDao extends JBaseDao {
    public UiFloorDao() {
        super("ui_floor");
    }

    public PageResult queryUiFloor(JdataBean parms, int size, int pageno) throws Exception {
        String sql = "select * from ui_floor where state='UNAVAILABLE' OR state='AVAILABLE' order by sort_value desc";
        List parmlist = new ArrayList();
        return getSession().query2(sql, parmlist, size, pageno);
    }

    public void upDateUiFloorStatus(String uifroolid,String state) throws Exception{
        getSession().update("update ui_floor set state=? where id=?", new Object[]{state, uifroolid});
    }
    public void updateUifloorPanelState(String uifroolpenleid,String state) throws Exception{
        getSession().update("update ui_floor_panel set state=? where id=?",new Object[]{state,uifroolpenleid});
    }

//    public PageResult queryPanel(JdataBean parms,int ui_floor_id,int size,int pageno) throws Exception {
//        String sql = "select * from ui_floor_panel where ui_floor_id=? order by sort_value desc";
//        return getSession().query(sql,new Object[]{ui_floor_id},size,pageno);
//    }

//    public List<JdataBean> queryPanel(String ui_floor_id) throws Exception{
//        return getSession().query("select * from ui_floor_panel where ui_floor_id=? order by sort_value desc", new Object[]{ui_floor_id});
//    }
    public List<JdataBean> queryPanel(String ui_floor_id) throws Exception {
        return getSession().query("select * from ui_floor_panel where ui_floor_id=? order by sort_value desc", new Object[]{ui_floor_id});
    }

    public List queryPanelProducts(String floorid) throws Exception {
        return getSession().query("select * from ui_floor_item where ui_floor_panel_id=? order by sort_value desc", new Object[]{floorid});
    }

    public JdataBean getFloorItem(String id) throws Exception {
        return getSession().getObject("SELECT m.title name,m.ui_floor_id floorid,m.id id from ui_floor_panel m WHERE m.id=?", new Object[]{id});
    }

    public PageResult queryFloorProduct(JdataBean parms, int size, int pageno) throws Exception {
        String sql = "select t.*,CONCAT(p.id,'_',t.id) idcode,p.unitname,p.code productcode,p.name productname,p.attrvals,s.filepath photourl,s.viewpath photoview,sm.filepath product_photourl,sm.viewpath product_photoview"
                + " from ui_floor_product f"
                + " inner join wb_products_prices t on t.productid = f.wb_product_id"
                + " inner join wb_products p on p.id=f.wb_product_id"
                + " left join app_pictures s on s.id=t.photo"//销售属性主图片
                + " left join app_pictures sm on sm.id=p.photo"//商品主图片
                + " where p.status<>-1";
        List parmlist = new ArrayList();
        String order = "";
        if (parms != null) {
            if (StringUtils.isNotBlank(parms.getString("itemid"))) {
                sql += " and f.ui_floor_panel_id=?";
                parmlist.add(parms.getString("itemid"));
            }
        }
        if (StringUtils.isNotBlank(order)) sql += " order by " + order;
        else sql += " order by f.sort_value";
        PageResult pr = getSession().query2(sql, parmlist, size, pageno);
        List<JdataBean> productlist = pr.getList();
        for (JdataBean product : productlist) {
            String photo = product.getString("photoview");
            if (StringUtils.isBlank(photo)) photo = product.getString("product_photoview");
            product.put("photo", photo);
            product.put("fullname", product.getString("productname") + " " + product.getDefault("name", "").toString());
        }
        return pr;
    }

    public String saveFloorItem(JdataBean itembean) throws HibernateException, Exception {
        if (StringUtils.isNotBlank(itembean.getString("id"))) {
            Map key = new HashMap();
            key.put("id", itembean.getString("id"));
            getSession().update("wb_floor_item", itembean.getDataMap(), key);
            return itembean.getString("id");
        } else
            return getSession().saveReturnKey("wb_floor_item", itembean.getDataMap());
    }

    public void delFloorItemProducts(String itemid) throws Exception {
        getSession().update("delete from ui_floor_product where ui_floor_panel_id=?", new Object[]{itemid});
    }

    public void saveFloorItemProducts(String itemid, String[] products) throws Exception {
        if (products != null) {
            for (int i = 0; i < products.length; i++) {
                String[] ss = products[i].split("_");
                getSession().update("insert into ui_floor_product(ui_floor_panel_id,wb_product_id,sort_value) values (?,?,?)", new Object[]{itemid, ss[0], i});
            }
        }
    }

}
